package controllers

import (
	"gdshop-back-end-go/client-api/app/request/MemberReq"
	"gdshop-back-end-go/client-api/app/service/MemberService"
	"gdshop-back-end-go/common/libs/token"
	"gdshop-back-end-go/common/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

type MemberController struct {
	*BaseController
}

func (c *MemberController) Init(r *ghttp.Request) *response.JsonResponse {
	memberId = token.GetLoginMemberId(r)
	if memberId < 1 {
		if r.URL.Path != "/member/index" {
			return response.FailByRequestMessage(r, "获取会员ID失败")
		}
	}
	return response.SuccessByRequest(r)
}

func (c *MemberController) Index(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.Index
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	if parames.MemberId < 1 {
		// 找不到登陆信息，不过不影响使用
		return response.JsonByAll(r, 10004, "成功", g.Map{
			"not_login": 1,
		})
	}
	return MemberService.Index(r.GetCtx(), parames)
}

func (c *MemberController) WechatBind(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.WechatBind
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.WechatBind(r.GetCtx(), parames)
}

func (c *MemberController) MyFavorite(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.MyFavorite
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.MyFavorite(r.GetCtx(), parames)
}

func (c *MemberController) MyBalance(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.MyBalance
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.MyBalance(r.GetCtx(), parames)
}

func (c *MemberController) DelFavorite(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.DelFavorite
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.DelFavorite(r.GetCtx(), parames)
}

func (c *MemberController) MySearchs(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.MySearchs
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.MySearchs(r.GetCtx(), parames)
}

func (c *MemberController) ClaerMySearchs(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.ClaerMySearchs
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.ClaerMySearchs(r.GetCtx(), parames)
}

func (c *MemberController) MyRefundList(r *ghttp.Request) *response.JsonResponse {
	var parames *MemberReq.MyRefundList
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return MemberService.MyRefundList(r, parames)
}
